package com.chinasoft.mall.util;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class SystemUtil {
	
    public static void openKeyboard(final Context cx) {

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                @Override
                public void run() {
                        InputMethodManager imm = (InputMethodManager)cx.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

                }
        }, 500);
   }
    //关闭软键盘
    public static void closeKeyboard(Context cx,View v) {
        InputMethodManager imm = (InputMethodManager) cx.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
   }

    public static final String getImei(Context cx) {
    	TelephonyManager tm = (TelephonyManager) cx
    	.getSystemService(Context.TELEPHONY_SERVICE);
    	
    	String imei = tm.getDeviceId();
    	return imei == null ? "000000000000000" : imei;
    }
    
	public static final String getversionId(Context cx) {
		PackageManager pm = cx.getPackageManager();  
        PackageInfo info = pm.getPackageArchiveInfo(cx.getPackageCodePath(), PackageManager.GET_ACTIVITIES);  
        String version=info.versionName;       //得到版本信息    
		return version == null ? "0.0.0.0" : version;
	}

	/**
	 * 获取屏幕宽度
	 * 
	 * @param cx
	 * @return
	 */
	public static final int getWidth(Context cx) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = cx.getApplicationContext().getResources().getDisplayMetrics();

		int screenWidth = dm.widthPixels;

		return screenWidth;

	}

	/**
	 * 获取屏幕高度
	 * 
	 * @param cx
	 * @return
	 */
	public static int getHeight(Context cx) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = cx.getApplicationContext().getResources().getDisplayMetrics();

		int screenHeight = dm.heightPixels;

		return screenHeight;
	}
	
	   /**     * 程序是否在前台运行     *     
	    *  * @return     */  
	public static boolean isAppOnForeground(Context cx) {   
		// Returns a list of application processes that are running on the    
		// device             
		ActivityManager activityManager = (ActivityManager) cx.getSystemService(Context.ACTIVITY_SERVICE); 
		String packageName = cx.getPackageName();     
		List<RunningAppProcessInfo> appProcesses = activityManager       
		.getRunningAppProcesses();    
		if (appProcesses == null)         
			return false;      
		for (RunningAppProcessInfo appProcess : appProcesses) {      
			// The name of the process that this object is associated with.    
			if (appProcess.processName.equals(packageName)         
					&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {     
				return true;      
				}    
			}      
		return false;     
	}
}
